package com.yzy.fly.type;

import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.Date;

public enum JDBCType {
    TINYINT("tinyint", Boolean.class.getName()),
    SMALLINT("smallint", "java.lang.byte"),
    MEDIUMINT("mediumint", "java.lang.byte"),
    INT("int", Integer.class.getName()),
    INTEGER("integer", Integer.class.getName()),
    BIGINT("bigint", Long.class.getName()),
    FLOAT("float", Float.class.getName()),
    DOUBLE("double", Double.class.getName()),
    DECIMAL("decimal", BigDecimal.class.getName()),
    DATE("date", Date.class.getName()),
    TIME("time", Date.class.getName()),
    YEAR("year", Date.class.getName()),
    DATETIME("datetime", Date.class.getName()),
    TIMESTAMP("timestamp", Date.class.getName()),
    CHAR("char", String.class.getName()),
    VARCHAR("varchar", String.class.getName()),
    TINYBLOB("tinyblob", String.class.getName()),
    TINYTEXT("tinytext", String.class.getName()),
    BLOB("blob", String.class.getName()),
    TEXT("text", String.class.getName()),
    MEDIUMBLOB("mediumblob", "java.lang.byte"),
    MEDIUMTEXT("mediumtext", "java.lang.byte"),
    LONGBLOB("longblob", "java.lang.byte"),
    LONGTEXT("longtext", String.class.getName()),
    JSON("json", "json");

    @Setter
    @Getter
    private String type;
    @Setter
    @Getter
    private String value;

    JDBCType(String type, String value) {
        this.type = type;
        this.value = value;
    }

    public static JDBCType type(String type) {
        if (StringUtils.isBlank(type)) {
            return JDBCType.VARCHAR;
        }
        JDBCType jdbcType = null;
        JDBCType[] values = JDBCType.values();
        try {
            for (int i = 0; i < values.length; i++) {
                JDBCType value = values[i];
                if (value.getType().equalsIgnoreCase(type)) {
                    jdbcType = value;
                    break;
                }
            }
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
            type = null;
        }
        return jdbcType;
    }


}
